-- RewardOpenCardView
local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.RewardDrawPanel_Scene_card

local ShakeConfig = require("app.configs.ShakeConfig")
local BoxRender = require("app.reward.views.renders.BoxRender")							
local Armature = app:getClass("Armature")

local Reward_Max_Count = 5
local Max_Quality_Count = 6
local Color_String = "activityCardColor%d"
local Card_Image_Path = "art/activity_icon/welfare_card_0%d.png"
local Card_Open_Light_Path = "art/activity_icon/CardLight_0%d.png"

local string_format = string.format

local QualityColor = {
    cc.c3b(176, 176, 176), --白色
    cc.c3b(117, 211, 124),  --绿色
    cc.c3b(108, 158, 239),  --蓝色
    cc.c3b(190, 76, 255),  --紫色
    cc.c3b(255, 154, 55),   --橙色
}

function ClassRef:init()
	self._ui["Panel/Image_73"]:setContentSize(display.size)
	self._card = self._ui["Panel/card"]
	self._card:findChild("Panel/tips_0"):setVisible(false)
	self._card:findChild("Panel/Panel_lock"):setVisible(false)
	self._card_ImageS = self._card:findChild("Panel/Image_s")
    self._card_ImageBack = self._card:findChild("Panel/img_back")
	self._buttonPanel = self._ui["Panel/Panel_open"]		

	self._card:getTimeline():gotoFrameAndPause(0)
	self._ui.Button:addClickEventListener(function()	
		self:openCard()		
	end)	
	self:setButtonVisible(false)
end

function ClassRef:show()
	self:setVisible(true)
	self._card:setVisible(true)
	self._buttonPanel:setVisible(false)	
end

function ClassRef:setData(data, info)
	self._data = data
	self._conf = GD:getShopDrawInfoById(info.id)
	self:refresh()
end

function ClassRef:refresh()
	if not self._data then return end
	local data = self._data
	if self._conf then
		local iconPath = string_format("%s%s.png", Res.activityIconPath, self._conf.cardIcon)
		self._card_ImageBack:loadTexture(iconPath)
	end
	local quality = data.quality
	if quality <= Max_Quality_Count then
		local path = string_format(L(Card_Image_Path), quality)
		self._card_ImageS:loadTexture(path)
		local tempStr = string_format(Color_String, quality)
		self._card:findChild("Panel/Panel_card/text"):setString(string_format(L("activityCardTips4"), L(tempStr)))

		path = string_format(L(Card_Open_Light_Path), quality)
		self._card:findChild("Panel/light"):setTexture(path)
		self._card:findChild("Panel/newwar_light_big2_1"):setTexture(path)
	end
	-- 抽卡收到服务器消息返回消息之后再开始播放翻转动画
	self:playCardRollEffect()
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

function ClassRef:openCard()
	self._card:setVisible(false)		
	self._buttonPanel:setVisible(false)	
	
	local bg = ccui.ImageView:create(Res.BoxBg)
	bg:ignoreContentAdaptWithSize(false)
	bg:setContentSize(display.size)
	bg:setPosition(display.cx, display.cy)
	self:addChild(bg)
	
	self._root:removeFromParent()
	self._root, self._uiBinding = cc.CSLoader:createNode(Res.BoxOpenScene, true, true, true)
	self._root:setContentSize(display.width, display.height)
	self._root:setAnchorPoint(0.5, 0.5)
	self._root:setPosition(display.width/2, display.height/2)
	display.setAutoScale(self._root)
	ccui.Helper:doLayout(self._root)
	
	self._root:setName(self:getViewName())
	self._ui = display.uiDelegate(self._root)
	self:addChild(self._root, 1)
	
	for i = 1, Reward_Max_Count do
		-- 资源里一共有5个panel，只显示第三个
		if i ~= 3 then
			local panel = self._ui["Panel/Panel_" .. i]
			panel:setVisible(false)
			
			local panel_open = self._ui["Panel/Panel_open_" .. i]
			panel_open:setVisible(false)
        else
            local panel = self._ui["Panel/Panel_" .. i]
		end
	end	
    -- 省略最后一步
	self:_openAllCard()
end

function ClassRef:_openAllCard()
    if not self._data then return end
	local conf = ShakeConfig:getConfig(11)
	display.nodeShake(self, conf)

	self._ui.Button:setVisible(false)
	local panel_3 = self._ui["Panel/Panel_3"] 
	panel_3:setVisible(false)
	
    local panel = self._ui.Panel
	local posX = panel_3:getPositionX()
	local posY = panel_3:getPositionY()
    local quality = self._data.quality or 1
	
	local params = {
        armatureName = "kbx_zkdh",
        path = "armatures/effect/kbx_zkdh"
    }
    local armBg = Armature.create(params)
	armBg:setPosition(posX, posY)
	panel:addChild(armBg, 1)
	
    local ani = {
		name = "kbx_zkdh",
		playTimes = 1,
		completeCallback = function()
		local action = cc.FadeIn:create(5/24)
		local panel_open_3 = self._ui["Panel/Panel_open_3"]
		panel_open_3:setVisible(true)
		panel_open_3:setOpacity(0)
		panel_open_3:runAction(action)
	end}
	armBg:playAni(ani)

    -- 根据品质替换不同底板
    if quality > 1 then
        local fileName = "kbx_bg" .. quality
        local params = {
            armatureName = "dh/bg1",
            path = "armatures/effect/" .. fileName
        }
        local arm1 = Armature.create(params)
        armBg:setSlotArmature("xbnxdf", arm1)

        local params = {
            armatureName = "dh/bg2",
            path = "armatures/effect/" .. fileName
        }
        local arm2 = Armature.create(params)
        armBg:setSlotArmature("xbzsd", arm2)
    end

    -- 上层特效
    local params = {
        armatureName = "kbx_zktx",
        path = "armatures/effect/kbx_zktx"
    }
    local armFg = Armature.create(params)
    armFg:setPosition(posX, posY)
    panel:addChild(armFg, 3)
    local ani = {
        name = "kbx_zktx",
        playTimes = 1,
        completeCallback = function(sender)
            sender:removeFromParent()
            self:_openBoxOver()
        end
    }
    armFg:playAni(ani)

    if quality > 1 then
        armFg:setColor(Enums.QualityColor[quality])
    end

    -- setData
	local panel_open_3 = self._ui["Panel/Panel_open_3"] 
	if panel_open_3 then
		local render = BoxRender.create(nil, panel_open_3)
        render:setLocalZOrder(2)
		render:setData(self._data)
	end 
end

function ClassRef:_openBoxOver()
    self._root:getTimeline():gotoFrameAndPlay(0, true)

    display.uiAddClick(self._ui.Panel, function()
        executeFunc(self._onOperate, "getMiraculousCardInfo", {self._data.uuid})
        self:closeView()
    end)
end

function ClassRef:playCardRollEffect()
    local timeline = self._card:getTimeline()
	timeline:setLastFrameCallFunc(function()
			self._card_ImageS:setVisible(true)
			self._buttonPanel:setVisible(true)	
			self._buttonPanel:getChildByName("Text_name_1"):setVisible(true)
	end)
    timeline:play("light", false)
end

function ClassRef:setButtonVisible(aVisible)
	self._ui["Panel/Panel_open/bg"]:setVisible(false)
	self._ui["Panel/Panel_open/icon"]:setVisible(false)
	self._ui["Panel/Panel_open/Text_name"]:setVisible(false)
	self._ui["Panel/Panel_open/num"]:setVisible(false)
	self._ui["Panel/Panel_open/tips_time"]:setVisible(false)
	self._ui["Panel/tips"]:setVisible(false)
end

return ClassRef